import Loading from '@/components/loading';
import http from '@/common/http';
import { Dialog } from 'mand-mobile';

const state = {
  historyStations: null
};
const getters = {
  getHistoryStations(state) {
    return state.historyStations;
  }
};
const actions = {
  getHistoryStations(context, params) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/order/historyStations', params).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setHistoryStations', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  }
};
const mutations = {
  setHistoryStations(state, data) {
    let arr = [];
    if (data.length > 0) {
      data.forEach((item, index) => {
        let obj = null;
        if (item.type === '2') {
          obj = {
            stationName: item.position,
            position: item.detailedAddress,
            coordinate: item.coord.split(',')
          };
        } else if (item.type === '1') {
          obj = {
            stationName: item.position,
            position: item.detailedAddress,
            coordinate: item.coord.split(','),
            stationId: item.stationId
          };
        }
        arr.push(obj);
      });
    }
    state.historyStations = arr;
  }
};
export default {
  state,
  getters,
  actions,
  mutations
};
